package com.hnue.k60b.models;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.TextView;
import android.text.Html;
import android.text.Spanned;


//object chứa các item gồm 4 thành phần title, des, link và date
public class RssItem {

	private String title;
	private String description;
	private String link;
	private String pubdate;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	public Spanned getDescription()
	{
		String temp = this.description;
		String dess ="";
		int img=temp.indexOf("<img");
		int link=temp.indexOf("<A");
		if (img !=-1)
		{
			dess=temp.replaceAll("<img.+?>", "");
		}
		Spanned result = Html.fromHtml(dess.trim());
		return result;
	}

	public String getDescription1() {
		//return description;
		String des;
		des = this.description;
		String standDescription="anc";
		int a=des.indexOf("<a href");
		int A=des.indexOf("<A"); // Hoac la: <A
		int a_end=des.indexOf("/a>");
		int br=des.indexOf("<br");
		int brs=des.indexOf("<BR");
		int img=des.indexOf("<img");
		
		
		if(a!=-1)//Neu' co' <a href
		{
			if(br!=-1)//Neu' co the <BR và thẻ <BR
			{	
				standDescription=des.substring(a_end+3,br);
			}
			else
			{	
				if(A!=-1)//Neu' co' the <A
				{
					standDescription=des.substring(a_end+3,A);
				}
				else
					standDescription=des.substring(a_end+3);
			}
		
			if(brs!=-1)//Neu' co the <BR
			{	
				standDescription=des.substring(a_end+3,brs);
			}
			
		}
		else
		{
			if(br!=-1)
			{	
				standDescription=des.substring(0, br);
			}
			else
			{	
				if(A!=-1)
				{
					standDescription=des.substring(0,A);
				}
				else
					standDescription=des.substring(0,des.length()-1);
				
			}			
		}	
		return standDescription;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLink() {
		
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}

	public String getDate() {
		
		System.out.print(pubdate);
		return pubdate;
	}

	public void setDate(String date) {
		this.pubdate = date;
	}
	
	public String getStandLinkImage(String description)
	{
		String linkImage="";
		int i=description.indexOf("src=");
		int j=description.indexOf("\"", i+5);
		
		if(i!=-1 && j!=-1)
		{
			linkImage=description.substring(i+5, j);
		}
		return linkImage;
		
	}

	public Bitmap getImage(String description)
	{
		String standLinkImage=getStandLinkImage(description);
		Bitmap bitmap=null;
		if(standLinkImage!="")
		try 
		{
			bitmap=BitmapFactory.decodeStream(new URL(standLinkImage).openConnection().getInputStream());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return bitmap;
	}	
	
}
